package nine.test_9_14;

public class Solution2 {
    public int pickGoods(int[] goods_list, int balance) {
        // write code here
        int n = goods_list.length;
        boolean[][] dp = new boolean[n + 1][balance + 1];
        dp[0][0] = true;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= balance; j++) {
                dp[i][j] = dp[i - 1][j];
                if(j >= goods_list[i - 1]) {
                    dp[i][j] = dp[i][j] || dp[i - 1][j - goods_list[i - 1]];
                }
            }
        }
        for (int j = balance; j >= 0; j--) {
            if(dp[n][j]) {
                return  j;
            }
        }
        return 0;
    }
}
